﻿using netDxf;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace MCPlatform.Dxf.Utils
{
    public class Vector3ToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Vector3 vector)
            {
                string formattedX = FormatNumber(vector.X);
                string formattedY = FormatNumber(vector.Y);
                string formattedZ = FormatNumber(vector.Z);
                return $"X: {formattedX}, Y: {formattedY}, Z: {formattedZ}";
            }
            return value;
        }


        private string FormatNumber(double number)
        {
            string numberString = number.ToString("G", CultureInfo.InvariantCulture);
            int decimalPointIndex = numberString.IndexOf('.');

            if (decimalPointIndex >= 0)
            {
                string fractionalPart = numberString.Substring(decimalPointIndex + 1);
                if (fractionalPart.Length > 3)
                {
                    return number.ToString("F3", CultureInfo.InvariantCulture);
                }
            }
            return numberString;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
